<template>
  <div class="app-container">
    <!--表单-->
    <el-form label-width="120px">
      <el-form-item label="借款额度">
        <el-input-number v-model="integralGrade.borrowAmount" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间开始">
        <el-input-number v-model="integralGrade.integralStart" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间结束">
        <el-input-number v-model="integralGrade.integralEnd" :min="0" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
        >
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import integralGradeApi from '@/api/core/integral-grade'
export default {
  name: 'Form',
  data() {
    return {
      integralGrade: {}, // 表单数据
      saveBtnDisabled: false // 保存按钮
    }
  },

  created() {
    if (this.$route.params.id) {
      this.fetchDataById(this.$route.params.id)
    }
  },

  methods: {
    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (this.integralGrade.borrowAmount && this.integralGrade.borrowAmount > 999999999) {
        this.$message.error('借款额度数值过大')
        this.saveBtnDisabled = false
        return
      }
      if (this.integralGrade.integralStart > 2147483647) {
        this.$message.error('积分区间开始数值过大')
        this.saveBtnDisabled = false
        return
      }
      if (this.integralGrade.integralEnd > 2147483647) {
        this.$message.error('积分区间结束数值过大')
        this.saveBtnDisabled = false
        return
      }

      if (!this.integralGrade.id) {
        this.saveData()
      } else {
        this.updateData()
      }
      this.saveBtnDisabled = false
    },

    saveData() {
      integralGradeApi.save(this.integralGrade).then(response => {
        this.$message({
          type: 'success',
          message: response.message
        })
        this.$router.push('/core/integral-grade/list')
      })
    },

    fetchDataById(id) {
      integralGradeApi.getById(id).then(response => {
        this.integralGrade = response.data.record
      })
    },

    updateData() {
      integralGradeApi.updateById(this.integralGrade).then(response => {
        this.$message({
          type: 'success',
          message: response.message
        })
        this.$router.push('/core/integral-grade/list')
      })
    }

  }
}
</script>

<style scoped>

</style>
